package com.quasar.hpatient.module.home_medicine;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.quasar.hpatient.R;
import com.quasar.hpatient.api.HttpClient;
import com.quasar.hpatient.api.HttpParams;
import com.quasar.hpatient.bean.home_medicine.MedicineBean;
import com.quasar.hpatient.module.home_medicine_edit.MedicineEditActivity;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;

import io.reactivex.Observable;
import lib.quasar.base.frame.BasePresenter;
import lib.quasar.base.frame.OnModelAcceptChangeListener;
import lib.quasar.context.BaseApp;
import lib.quasar.recycler.BaseCommonAdapter;
import lib.quasar.recycler.holder.RecyclerHolder;
import lib.quasar.recycler.manager.CrashLinearLayoutManager;
import lib.quasar.util.DimenUtil;
import lib.quasar.util.SpannableUtil;
import okhttp3.RequestBody;

/**
 * description: 服药单
 * created by kalu on 2018/5/3 14:07
 */
public class MedicinePresenter implements BasePresenter {

    private final LinkedList<MedicineBean.MedicineItemBean> mDatas = new LinkedList<>();
    public MedicineBean mDatasModel = new MedicineBean();

    void initList(final MedicineView view, RecyclerView recycler) {

        final BaseCommonAdapter<MedicineBean.MedicineItemBean> adapter = new BaseCommonAdapter<MedicineBean.MedicineItemBean>(mDatas, R.layout.activity_home_medicine_child) {

            @Override
            protected void onNext(RecyclerHolder holder, final MedicineBean.MedicineItemBean model, final int position) {

                final String mname = model.getMname();
                final String dosename = model.getDose_name();
                final String name = BaseApp.getContext().getResources().getString(R.string.home_medicine_list_title, mname, dosename);
                view.setText(holder, R.id.activity_home_medicine_child_name, name);
                holder.setOnClickListener(R.id.activity_home_medicine_child_name, v -> jumpEdit(view, position));
                holder.setOnClickListener(R.id.activity_home_medicine_child_edit, v -> jumpEdit(view, position));
                holder.setOnClickListener(R.id.activity_home_medicine_child_info, v -> jumpEdit(view, position));


                SpannableUtil.append(model.getMethod(), Color.BLACK, 1.05f);
                SpannableUtil.newline();

                final String remark = model.getRemark();
                if (!TextUtils.isEmpty(remark)) {
                    final String temp = BaseApp.getStrings(R.string.home_medicine_remark, remark);
                    SpannableUtil.append(temp, Color.GRAY, 1.05f);
                    SpannableUtil.newline();
                }

                final List<MedicineBean.MedicineItemBean.MedicineItemDetailBean> list = model.getUse_detail();
                if (null != list || list.size() > 0) {
                    for (MedicineBean.MedicineItemBean.MedicineItemDetailBean temp : list) {
                        final int order = temp.getOrder();
                        final String time = temp.getShortUse_time();
                        final String number = String.valueOf(temp.getUse_number());
                        int id = order < 10 ? R.string.home_medicine_records : R.string.home_medicine_record;
                        final String record = BaseApp.getStrings(id, order, time, number, dosename);
                        SpannableUtil.append(record, Color.GRAY, 0.95f);
                        if (list.indexOf(temp) == (list.size() - 1)) continue;
                        SpannableUtil.newline();
                    }
                    view.setText(holder, R.id.activity_home_medicine_child_info, SpannableUtil.build());
                }
            }
        };

        final View foot = LayoutInflater.from(BaseApp.getContext()).inflate(R.layout.activity_home_medicine_foot, null);
        final int height = DimenUtil.dp2px(50);
        RecyclerView.LayoutParams params = new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, height);
        foot.setLayoutParams(params);
        adapter.addFoot(foot);

        adapter.setNullView(BaseApp.getContext(), R.layout.layout_recycler_loading);
        final CrashLinearLayoutManager manager = new CrashLinearLayoutManager(BaseApp.getContext());
        view.setRecycler(recycler, adapter, manager);
    }

    void medicineList(final MedicineView view, RecyclerView recycler) {

        final HashMap<String, Object> map = new HashMap<>();
        map.put(PRESCRIPTION_DATE, getDate());


        final RequestBody params = createParams(map, HttpParams.DATA_TYPE_5_2);
        final Observable observable = HttpClient.getSingleHolder().getHttpService().medicineList(params);

        request(observable, new OnModelAcceptChangeListener<MedicineBean>() {
            @Override
            public void modelSucc(MedicineBean model) {

                if (null == model) {
                    modelFail();
                    return;
                }

                mDatasModel = model;

                final List<MedicineBean.MedicineItemBean> list = model.getDatas();
                if (null == list || list.size() == 0) {
                    modelFail();
                    return;
                }

                mDatas.clear();
                mDatas.addAll(list);
                view.refreshList(recycler);
            }

            @Override
            public void modelFail() {
                view.setListNull(recycler, R.layout.layout_recycler_fail);
                view.refreshList(recycler);
            }
        });
    }

    void addMedicine(final MedicineView view, RecyclerView recycler, MedicineBean.MedicineItemBean model) {

        if (null == view || null == recycler || null == model) return;

        mDatas.addFirst(model);
        view.refreshList(recycler);
    }

    void modifyMedicine(final MedicineView view, RecyclerView recycler, MedicineBean.MedicineItemBean model, int position) {

        if (null == view || null == recycler || null == model || position == -1) return;

        mDatas.set(position, model);
        view.refreshList(recycler);
    }

    void deleteMedicine(final MedicineView view, RecyclerView recycler, int position) {

        if (null == view || null == recycler || position == -1) return;

        mDatas.remove(position);
        view.refreshList(recycler);
    }

    void jumpEdit(final MedicineView view, int position) {
        final Context context = BaseApp.getContext();
        final Intent intent = new Intent(context, MedicineEditActivity.class);
        intent.putExtra(MedicineActivity.DATAS, mDatas);
        intent.putExtra(MedicineActivity.PATIENTID, mDatasModel.getPatientid());
        intent.putExtra(MedicineActivity.POSITION, position);
        Log.e("SSSSSSSS", mDatas.toString());
        Log.e("SSSSSSSS--aaa----", mDatasModel.getPatientid() + "");
        view.goEdit(intent);
    }

    @Override
    public void recycler() {
    }
}
